﻿using Alumnos.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Alumnos.Controllers
{
    public class MateriaController : Controller
    {
        private AlumnosContext context = new AlumnosContext();

        public ActionResult Index()
        {
            IEnumerable<Materia> materias = context.Materias;
            return View(materias);
        }

        [HttpGet]
        public ActionResult Agregar()
        {
            return View(new Materia());
        }

        [HttpPost]
        public ActionResult Agregar(Materia materia)
        {
            if (ModelState.IsValid)
            {
                context.Materias.Add(materia);
                context.SaveChanges();
                return Redirect("Index");
            }

            return View(materia);
        }

        [HttpGet]
        public ActionResult Editar(int id)
        {
            Materia alumno = context.Materias.Find(id);
            return View(alumno);
        }

        [HttpPost]
        public ActionResult Editar(Materia materia)
        {
            if (ModelState.IsValid)
            {
                context.Entry(materia).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(model: materia);
        }

        public ActionResult Borrar(int id)
        {
            Materia materia = context.Materias.Find(id);
            context.Materias.Remove(materia);
            context.SaveChanges();

            return RedirectToAction("Index");
        }
    }
}
